const util = require("../../utils/util")
const app = getApp()
Page({

  data: {
    placeHolder:'评论',

  },

  onLoad: function (options) {
    this.setData({
      openid : app.globalData.openid
    })
    // console.log(options.id)
    this.data.id = options.id
    this.getDeatil()
  },
  getDeatil(){
    var that = this;
    wx.cloud.callFunction({
      name:'lookUp',
      data:{
        collection:'menu_comments',
        from:'comment',
        localField:'_id',
        foreignField:'menu_commment_id',
        as:'commentList',
        match:{_id:that.data.id}
      },
      success:res=>{
      // console.log(res.result.list[0])
      var action = res.result.list[0]
      for(var l in action.commentList){
        for(var x in action.commentList[l].backingList)
          if(action.commentList[l].backingList[x].openid == app.globalData.openid){
            action.commentList[l].isPrized1 = true
          }
      }
      for(var l in action.commentList){
        for(var x in action.commentList[l].no_backingList)
          if(action.commentList[l].no_backingList[x].openid == app.globalData.openid){
            action.commentList[l].isPrized2 = true
          }
      }
      for(let l in action.commentList){
        action.commentList[l].time = util.formatTime(new Date(action.commentList[l].time))
      }
      that.setData({
        action:res.result.list[0]
      })
      }
    })
  },
  //发布评论
  getInputValue(e){
    // console.log(e.detail.value)
    this.data.inputValue = e.detail.value
  },
  publishComment(){
    var that = this;
    if(app.globalData.userInfo == null){
      wx.navigateTo({
        url: '/pages/auth/auth',
      })
    }else{
      wx.cloud.database().collection('comment').add({
        data:{
          menu_commment_id:that.data.id,
          nickName: app.globalData.userInfo.nickName,
          faceImg:app.globalData.userInfo.avatarUrl,
          openid : app.globalData.openid,
          text: that.data.inputValue,
          time:Date.now(),
          toOpenid : that.data.toOpenid,
          toNickname : that.data.toNickname,
          backingList : [],
          no_backingList : []
        },
        success(res){
            // console.log(res)
            wx.showToast({
              title: '评论成功！',
            })
            that.getDeatil()
            that.setData({
              inputValue : '',
              placeHolder:'评论'
            }) 
        }
      })
    }
  },
  //删除评论
  deleteComment(e){
    var that = this;
    wx.showModal({
      title:'提示',
      content:'确定要删除此评论吗？',
      success(res){
        // console.log(e.currentTarget.dataset.id)
        wx.cloud.database().collection('comment').doc(e.currentTarget.dataset.id).remove({
          success(res){
            // console.log(res)
            wx.showToast({
              title: '删除成功',
            })
            that.getDeatil()
          }
        })
      }
    })
    
  },
  //点击评论，在回复框出现回复名字
  huifuComment(e){
    var index = e.currentTarget.dataset.index
    this.setData({
      placeHolder : '回复' + this.data.action.commentList[index].nickName,
      toOpenid: this.data.action.commentList[index].openid,
      toNickname : this.data.action.commentList[index].nickName,
    })
  },
  //支持
  backingAction(e){

    var that = this;
    if(app.globalData.userInfo == null){
      wx.navigateTo({
        url: '/pages/auth/auth',
      })
    }else{
      // console.log(e.currentTarget.dataset.id)
      var that = this;
      wx.cloud.database().collection('comment').doc(e.currentTarget.dataset.id).get({
        success(res){
          // console.log(res)
          var action = res.data
          var tag = false
          var index
          for(var l in action.backingList){
            if(action.backingList[l].openid == app.globalData.openid){
              tag = true
              index = l
              break
            }
          }

          if(tag){
            //解决手机取消点赞时候设置为null的bug
            let backingList = []
            for(let i in action.backingList){
              if(index != i){
                backingList.push(action.backingList[i])
              }
            }
            // console.log(action.backingList)
            wx.cloud.database().collection('comment').doc(e.currentTarget.dataset.id).update({
              data: {
                backingList:backingList
              },
              success(res){
                // console.log(res)
                that.getDeatil()
              }
            })
          }else{
            //之前未点赞 添加点赞记录
            var user = {}
            user.nickName = app.globalData.userInfo.nickName
            user.faceImg = app.globalData.userInfo.avatarUrl
            user.openid = app.globalData.openid
            action.backingList.push(user)
            
            wx.cloud.database().collection('comment').doc(e.currentTarget.dataset.id).update({
              data:{
                backingList:action.backingList
              },
              success(res){
                // console.log(res)
                wx.showToast({
                  title: '支持成功！',
                })
                that.getDeatil()

               
              }
            })
          }

          

        }
      })
    }

  },
  //不支持
  no_backingAction(e){
    var that = this;
    if(app.globalData.userInfo == null){
      wx.navigateTo({
        url: '/pages/auth/auth',
      })
    }else{
      // console.log(e.currentTarget.dataset.id)
      var that = this;
      wx.cloud.database().collection('comment').doc(e.currentTarget.dataset.id).get({
        success(res){
          // console.log(res)
          var action = res.data
          var tag = false
          var index
          for(var l in action.no_backingList){
            if(action.no_backingList[l].openid == app.globalData.openid){
              tag = true
              index = l
              break
            }
          }

          if(tag){
            //解决手机取消点赞时候设置为null的bug
            let no_backingList = []
            for(let i in action.no_backingList){
              if(index != i){
                no_backingList.push(action.no_backingList[i])
              }
            }
            // console.log(action.backingList)
            wx.cloud.database().collection('comment').doc(e.currentTarget.dataset.id).update({
              data: {
                no_backingList:no_backingList
              },
              success(res){
                // console.log(res)
                that.getDeatil()
              }
            })
          }else{
            //之前未点赞 添加点赞记录
            var user = {}
            user.nickName = app.globalData.userInfo.nickName
            user.faceImg = app.globalData.userInfo.avatarUrl
            user.openid = app.globalData.openid
            action.no_backingList.push(user)
            
            wx.cloud.database().collection('comment').doc(e.currentTarget.dataset.id).update({
              data:{
                no_backingList:action.no_backingList
              },
              success(res){
                // console.log(res)
                // wx.showToast({
                //   title: '点赞成功！',
                // })
                that.getDeatil()

               
              }
            })
          }

          

        }
      })
    }
  },
})